var url;
$(document).ready(function(){
	
	validateAddForm();
})

function validateAddForm() {

	$("#typeAddForm").validate({
		onsubmit:true,// 是否在提交是验证
	　　	onfocusout:false,// 是否在失去焦点时验证
	　　	onkeyup :false,// 是否在敲击键盘时验证
		rules:{
			typeName: {
				required: true,
			},
			valueId:{
				required: true,
			},
			valueName: {
				required: true,
			},
		},
		messages: {
			
			typeName: {
				required: "类型名称必填",
			},
			valueId:{
				required: "类型对应数值必填",
			},
			valueName: {
				required: "类型描述必填",
			}		
		},
		
		errorPlacement: function(error, element){
			$("#add-errtip").removeClass("alert-info").addClass("alert-danger");
			$("#add-errtip").html(error);
		},
		submitHandler: function(form){
			form.submit();
		},
		invalidHandler: function(form, validator) {
			return false;}
		
	})
	/*
	 jQuery.validator.addMethod("codeFormat",function(value,element){
		 var reg = /^[A-Za-z_]+$/;
         return this.optional(element) || reg.test(value);
      }, "类型编号只包含字母或下划线或二者的组合!");*/
}



/**
 * 分页控制
 * @param frm 控制的目标表单
 * @param index	当前页下标
 * @returns
 */
function page_nav(frm, index){
	frm.pageIndex.value = index;
	frm.submit();
}

/**
 * 类别新增
 */
$(".typeadd").on("click", function(e){
	e.preventDefault();
	$("#addType").modal("show");
})


/**
 * 类别修改
 */
$(".typeedit").on("click", function(e){
	var obj = $(this);
	url = obj.attr("url");
	
	
	var typeCode = obj.attr("type-code");
	var valueId = obj.attr("type-valueid");
	
	var tip;
	
	$.get(url + "basic/type/modify", {"typeCode": typeCode, "valueId": valueId}, function(result){
		console.log(result);
		var data = result.msg;
		if (data != null && data != "fail") {
			//将值赋给模态框
			$("#m_typeCode").val(data.typeCode);
			$("#m_typeName").val(data.typeName);
			$("#m_valueId").val(data.valueId);
			$("#m_valueName").val(data.valueName);
			$("#m_createDate").val(data.createDate);
			$("#m_createBy").val(data.createBy);
		} else if (data == "fail") {
			tip = "{\"text\":\"无法获取物资类别" + typeCode + "信息,进行修改!\", \"layout\":\"center\",\"type\":\"error\"}";
			$("#error-tip").attr("data-noty-options", tip);
			$("#error-tip").trigger("click");
		} else if (result.error != null) {
			tip = "{\"text\":\""+ result.error +"\", \"layout\":\"center\",\"type\":\"error\"}";
			$("#error-tip").attr("data-noty-options", tip);
			$("#error-tip").trigger("click");
		}
		
		
	}, "json");
	

	//打开模态框
	e.preventDefault();
	$('#modifyType').modal('show');
	
})




